<!DOCTYPE html>
<html lang="en">
<head th:include="public/public_css::public_css ('用户管理')" />
<body style="background-color: #f2f2f2;">
<div class="layui-fluid larry-wrapper">
    <div class="layui-card">
        <div class="layui-row layui-col-space30">
            <div class="layui-col-xs24">
                <form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
                    <div class="layui-inline">
                        <label class="layui-form-label">编号</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="bh" id="code" placeholder="编号" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="time" id="createTime" placeholder=" - ">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">供应商名称</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="gysmc" id="supplierName" placeholder="供应商名称" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">PO状态</label>
                        <div class="layui-input-inline">
                            <!--<input type="text" class="layui-input" name="zt" id="POType" placeholder="PO状态" />-->
                            <select name="zt" id="POType" >
                                <option></option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">审核状态</label>
                        <div class="layui-input-inline">
                            <!--<input type="text" class="layui-input" name="shzt" id="auditType" placeholder="审核状态" />-->
                            <select name="shzt" id="auditType" >
                                <option value="">全部</option>
                                <option value="0">未审核</option>
                                <option value="1">已审核</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline inline-search-btn">
                        <a class="layui-btn layui-btn-normal" onclick="queryTable(1)" id="query" data-type="reload">搜索</a>
                        <button type="reset" onclick="queryTable(0)" class="layui-btn layui-btn-primary" style="background-color: #1e9fff; color: white;">重置</button>
                    </div>
                </form>
                <!--顶部按钮展示区域 【 tpl-button 】 -->
                <div class="group-button tpl-button" id="toolbar"></div>
                <!--列表-->
                <section class="panel panel-padding layui-card-body" >
                    <div class="pro">
                        <table id="table"  class="table text-nowrap" style="word-break:break-all; word-wrap:break-word;"></table>
                    </div>
                </section>
            </div>
        </div>
    </div>
</div>
</body>
<style>
    .tooltip2{
        display: inline-block;
        margin-left: 1rem;
    }
    /*.tooltip2 > button{*/
        /*display: inline;*/
    /*}*/
</style>
<div th:include="public/public_js::public_js"></div>
<div th:include="public/public_btn::public_btn"></div>

<script th:inline="javascript">

    var functions = [[${functions}]];
    layui.use(['list','layer','zzpUtils','jquery','laydate', 'tablebutton'],function () {
        var zzpUtils= layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate,
            // 按钮引入
            tablebtn = layui.tablebutton;

        //初始化
        zzpUtils.webSocket.init();
        tablebtn.buildOnClick({
            type: 1,
            temp: $("#function_tpl"),
            functions: functions.headBtn,
            row: null,
            event: {
                // 查看
                searchEvent: function(data){
                    console.log("点查询了", data.row);
                    lookDialog(data.row);
                },
                /*新增*/
                addEvent: function (data) {
                    add();
                },
                /*编辑*/
                editEvent: function (data) {
                    if (data.row != undefined) {
                        editDialog(data.row.id);
                    }
                },
                /*标记收货完成*/
                confirmEvent: function (data) {
                    confirm();
                },
                /*删除*/
                deleteEvent: function (data) {
                    if (data.row != undefined) {
                        var ids = [];
                        ids.push(data.row.id)
                        deleteClick(ids);
                    } else {
                        var arr = [];
                        var $rows = $table.bootstrapTable('getSelections');
                        if ($rows.length > 0) {
                            arr = $.map($rows, function (row) {
                                return row.id;
                            })
                            deleteClick(arr);
                        }else{
                            layer.msg("至少选择一条数据");
                        }
                    }
                },
                /*导出*/
                exportEvent: function (data) {
                    var arr = [];
                    if (data.row != undefined) {
                        arr.push(data.row.id);
                    } else {
                        var $rows = $table.bootstrapTable('getSelections');
                        if ($rows.length > 0) {
                            arr = $.map($rows, function (row) {
                                return row.id;
                            })
                        }
                    }
                    /*
                     * 以下是将ids和对象拼接在一起
                     * */
                    var str = zzpUtils.getFormJson($("#searchForm"));
                    var object2 = Object.assign({ids: arr}, str);
                    /*将条件查询和ids传到后台*/
                    zzpUtils.outputXslx({
                        url: "/wmsPurchaseOrder/getOutPutList",
                        data: JSON.stringify(object2)
                    });
                },
                /*审核*/
                auditEvent: function (data) {
                    console.log('执行了审核');
                    audit();
                },
                /*附件管理*/
                accessoryEvent: function (data) {
                    console.log('点击了附件管理');
                    accessoryDialog(data.row.id);
                }
            }
        });

        // 查询交货方式
        zzpUtils.select({
            elem:"#POType",
            url:"/sysdictionary/getSysdictionary/wms_purchase_order_zt"
        });

        function add () {
            zzpUtils.dialog({
                title:"添加采购订单",
                type:2,
                content: "/inOperation/AddPurchase",
                btn:[],
                end: function () {
                    console.log('dialog关闭回调');
                    $('#table').bootstrapTable('refresh');
                }
            });
        }

        function editDialog(id){
            localStorage.id = id;
            zzpUtils.dialog({
                title:"编辑采购订单头信息",
                type:2,
                content: "/inOperation/EditPurchase",
                end: function () {
                    console.log('dialog关闭回调');
                    $('#table').bootstrapTable('refresh');
                }
            });
            console.log('dialog打开');
        }

        function lookDialog(row){
            console.log("触发了lookDialog", row.shzt);
            localStorage.id = row.id;
            localStorage.shzt = row.shzt;
            localStorage.gysid = row.gysid;

            zzpUtils.dialog({
                title:" 查看明细",
                type:2,
                content: "/inOperation/LookDetail",
                area: ['80%', '70%']
            });
        }

        function deleteClick(ids) {
            var Ids = {ids: ids};
            $.ajax({
                url: '/zxsswms/wmsPurchaseOrder/delete',
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(Ids),
                success: function (data) {
                    if(data.type === 'success') {
                        layer.msg(data.title);
                        $('#table').bootstrapTable('refresh');
                    } else {
                        layer.msg(data.title);
                    }
                }
            })
        }

        function audit() {
            var ids = [];
            var selectData = $table.bootstrapTable('getSelections');
            if (selectData.length > 0) {
                selectData.forEach(function (item) {
                    ids.push(item.id);
                });
                var data = {ids: ids};
                $.ajax({
                    url: '/zxsswms/wmsPurchaseOrder/updateShzt',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function (data) {
                        if (data.type === 'success') {
                            layer.msg(data.title);
                            $('#table').bootstrapTable('refresh');
                        }
                    }
                })
            } else {
                layer.msg('请选择至少一条数据');
            }
        }
        
        function confirm() {
            var ids = [];
            var selectData = $table.bootstrapTable('getSelections');
            if (selectData.length > 0) {
                selectData.forEach(function (item) {
                    ids.push(item.id);
                });
                var data = {ids: ids};
                $.ajax({
                    url: '/zxsswms/wmsPurchaseOrder/updateZt',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function (data) {
                        layer.msg(data.message);
                        if (data.type === 'success') {
                            $('#table').bootstrapTable('refresh');
                        }
                    }
                })
            } else {
                layer.msg('请选择至少一条数据');
            }
        }
        
        function accessoryDialog(id) {

            zzpUtils.dialog({
                title: "附件管理",
                type: 2,
                content: "/inOperation/accessory?id=" + id,
                area: ['80%', '80%'], //设置dialog大小，也可以不设置，
                end: function () {//增加成功后刷新
                    //zzpUtils.refreshTable("#table");
                }
            });
        }

        laydate.render({
            elem: '#createTime'
            ,range: true
        });
        // 主页面table
        var $table = zzpUtils.table({
            elem: '#table',
            isApi: true,
            pagination:true,
            isOperate: true,
            //functions: functions.tableBtn,
            url: '/wmsPurchaseOrder/list',
            columns: [
                {field: '', checkbox: true},
                {field: "gysmc", title: "供应商名称"},
                {field: "bh", title: "编号"},
                {field: "show_lx", title: "类型"},
                {field: "show_zt", title: "状态"},
                {field: "show_shzt", title: "审核状态"},
                {field: "zsl", title: "总数量"},
                {field: "yjdhsj", title: "计划到货时间"},
                {field: "bz", title: "备注"},
                ],
//            queryParams: function () {//查询条件
//                return  zzpUtils.getFormJson($("#searchForm"));
//            },
            operateFormatter: operateFormatter

        });

        /*表格右侧按钮*/
        function operateFormatter(value, row, index) {
            var btn = '', button = '', buttons1 = '', buttons2 = '';
            var buttons = '<a class="tooltip2" id="tooltip' + row.id + '" data-tooltip-content="#tool' + row.id + '">更多</a>'
                + '<div class="tooltip_templates"><span id="tool' + row.id + '">';
            for (var i = 0; i < functions.tableBtn.length; i++) {
                var item = functions.tableBtn[i];
                //更多之外
                if (item.event === 'searchEvent' || item.event === 'accessoryEvent') {
                    buttons1 += '<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="' + item.event + '">'
                        + item.name
                        + '</button>';;
                } else {
                    //更多之内
                    if (row.shzt === '1' && item.event !== 'exportEvent') {
                        // 禁用
                        btn = '<button class="layui-btn layui-btn-sm layui-btn-disabled" disabled lay-event="' + item.event + '">'
                            + item.name
                            + '</button>';
                    } else {
                        // 不禁用
                        btn = '<button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="' + item.event + '">'
                            + item.name
                            + '</button>';
                    }
                    buttons2 += btn;
                }
            }
            button += buttons1;
            button += buttons;
            button += buttons2;
            button += '</span></div>';
            return button;
        }

//        $(".inline-search-btn").on('click',function () {
//            zzpUtils.refreshTable("#table");
//        })

        //搜索按钮
        window.queryTable = function (val) {
            var formdata = $("form").serializeArray();
            var Datas = {};
            var cjsjstart = '',cjsjend = '';
            formdata.forEach(function (item) {
                Datas[item.name] = item.value;
            });
            if (Datas.time) {
                var start = Datas.time.substring(0, 10);
                var end = Datas.time.substring(12);
                var timestart = new Date(start);
                cjsjstart = timestart.getTime();
                var timeend = new Date(end);
                cjsjend = timeend.getTime();
            }
            var DATA = {};
            for (var key in Datas) {
                if(key !== 'time') {
                    if (val === 1) {
                        DATA[key] = Datas[key];
                    }else {
                        DATA[key] = "";
                    }
                }
            }
            if (val === 1) {
                DATA.cjsj_start = cjsjstart;
                DATA.cjsj_end = cjsjend;
            }else {
                DATA.cjsj_start = "";
                DATA.cjsj_end = "";
            }
            DATA.page = 1;
            DATA.rows = 10;
            $('#table').bootstrapTable('refresh',{
                query: DATA
            })
        }

    });
</script>

</html>